home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6827 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: C constant expression declarations
  5. Date: 15 Feb 1996 08:13:05 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4fvm2hINNa61@keats.ugrad.cs.ubc.ca>
  8. References: <31229735.41C67EA6@isi.com> <DMto56.Lo3@uns.bris.ac.uk>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <DMto56.Lo3@uns.bris.ac.uk>,
  12. Nathan Sidwell <nathan@pact.srf.ac.uk> wrote:
  13.  >: ...and so on.  Lately, I have been observing in code from other people
  14.  >: equivalent declarations such as:
  15.  >
  16.  >:  #define EXPR1   (1)
  17.  >:  #define EXPR2   (2)
  18.  >
  19.  >Whilst, as I'm sure you realise, it's necessary to put brackets round
  20.  >things like (a + b), it is not necessary around unsigned constants.
  21.  >However, a gotcha is using signed constants, for example
  22.  >
  23.  >#define    EXPR -1
  24.  >if(a EXPR) ...
  25.  
  26. Are you sure that C has signed constants? That is, is -1 lexically recognized
  27. as a constant, or as a minus token followed by an integer quantity? It's
  28. probably done the latter way, which means that "-1" is a compound expression.
  29. If it weren't parsed this way, a construct like (a-1) would be parsed as
  30. something like '(' IDENTIFIER INTEGER ')', a syntax error. I'm not sure what th
  31. C standard says, just what common-sense about compiler construction says. :)
  32.  
  33.  >this will complile as 'a - 1', rather than give a syntax error
  34.  >
  35.  >leaving out the brackets, will not affect correct code, but can cause
  36.  >incorrect code to be compiled to something undesired, rather than give
  37.  >an error. Having brackets o unsigned constants, just reinforces the habit.
  38.  >
  39.  >Even wih the brackets, I could still have silent error, for instance
  40.  >
  41.  >#define EXPR (-1)
  42.  >int a(int);
  43.  >if(a EXPR) ...
  44.  >
  45.  >will be compiled as 'a(-1)'. However this compounds two errors, using
  46.  
  47. But if you had #define EXPR (1), you still have the silent error. Thus this
  48. particular case does not hinge on whether or not you have a signed or
  49. unsigned constant.
  50.  
  51.  >a function name instead of a variable (I assume you meant to compare
  52.  >a variable with a value), and omit the comparison operator.
  53.  
  54. Parentheses in pre-processor macros unfortunately can't always protect you if
  55. you write an incorrect program, and it happens to parse as a valid sentence of
  56. the C language, especially if the pre-processor's expansion of your macros is
  57. not the cause of the problem.
  58. -- 
  59.  
  60.